﻿using AutoMapper;
using MediatR;
using Traceability.App.Api.Read.Application.Command.Kill;
using Traceability.App.Domain.养殖基本信息表;
using Traceability.App.Domain.屠宰信息表;
using Traceability.App.ErrorCode;
using Traceability.App.Intrastructure.Interface;

namespace Traceability.App.Api.Read.Application.Handler.Kill
{
    public class KillUpdateHandler : IRequestHandler<KillUpdateCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<KillInformationModel> KillRepo;
        private readonly IMapper mapper;

        public KillUpdateHandler(IBaseRepository<KillInformationModel> KillRepo, IMapper mapper)
        {
            this.KillRepo = KillRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(KillUpdateCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();
            var entity = mapper.Map<KillInformationModel>(request);
            var r = KillRepo.Update(entity).Result;
            if (r > 0)
            {
                res.Message = "修改成功";
                res.Code = ApiEnum.Ok;
                res.Data = r;
            }
            else
            {
                res.Message = "修改失败";
                res.Code = ApiEnum.Error;
                res.Data = r;
            }
            return Task.FromResult(res);
        }
    }
}
